home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / 2DLab / dist.m < prev    next >
Encoding:
Text File  |  1992-01-14  |  1.4 KB  |  42 lines

  1. #include "math.h"
  2. #include "stdio.h"
  3. #include "tspheaders.h"
  4. #include "TwoDView.h"
  5. #include "TwoDTSP.h"
  6. #include "prototypes.h"
  7.  
  8. /************************************************************************/
  9. /* This function calculates the distance between every pair of cities in
  10.    the problem. 
  11.  
  12.    The distance between city i and city j where i < j is stored only once
  13.    as k = i*NumberOfCities - i*(i+1)/2 + (j-i+1).  Hence, the length of the 
  14.    distance array is NumberOfCities*(NumberOfCities - 1)/2. 
  15. */
  16. /************************************************************************/
  17. /* Parameters:      float *Data: Pointer to area that contains coordinates 
  18.                              of cities.
  19.  
  20.                 float *Distance: Pointer to area that will contain the
  21.                                  distances between each pair of cities.
  22.  
  23.              int NumberOfCities: Number of cities in this tour.
  24. */
  25.  
  26. /************************************************************************/
  27. /************************************************************************/
  28. void CalculateDistance(float *Data,float *Distance,int NumberOfCities) 
  29. {
  30. int i,j,k;
  31.  
  32. /* calculate distance between every pair of points */
  33. for(i=0;i<NumberOfCities;i++) {
  34.   for(j=(i+1);j<NumberOfCities;j++) {
  35.     k = kfrom(i,j,NumberOfCities);
  36.     Distance[k] = (double)(square(x(i) - x(j)) 
  37.                 + square(y(i) - y(j)));
  38.     Distance[k] = sqrt((double)Distance[k]);
  39.   }
  40. }
  41. }
  42.